// 一个在node环境中读取json文件的小工具
const fs = require("fs")

function readFile (filePath) {
  try {
    const fileContent = fs.readFileSync(filePath, 'utf8')
    const jsonData = JSON.parse(fileContent)
    return jsonData
  } catch (error) {
    console.log('读取报错');
    throw error
  }
}

const filePath = 'node/file/goal.json'
const jsonData = readFile(filePath)
console.log(jsonData);

function formatData (data) {
  let list = []
  for (let key in data) {
    list.push(data[key].name)
  }
  return list
}

let mydata = formatData(jsonData)

fs.writeFile('node/file/result.json', JSON.stringify(mydata), 'utf8', err => {
  if (err) {
    console.log('发生错误了');
  } else {
    console.log('写入成功');
  }
})